package com.gacu.viewer.client.ui.display_area;

import com.gacu.viewer.client.rna.RNAssDrawable;
import com.google.gwt.user.client.Event;
import com.objetdirect.tatami.client.gfx.GraphicObject;
import com.objetdirect.tatami.client.gfx.GraphicObjectListener;

public class DrawerListener implements GraphicObjectListener {
    private boolean      holding;
    private int          x, y;
    private final Drawer dr;
    public RNAssDrawable rna;
    private boolean      rnaSet;

    public DrawerListener(Drawer dr) {
        this.dr = dr;
        rnaSet = false;
        this.holding = false;
    }

    @Override
    public void mouseClicked(GraphicObject graphicObject, Event event) {
        /*
         * for (RNAssDrawable rna :this.dr.drawedObjects){ if (rna.ContainVirtualGroup(graphicObject.getGroup())) { rna.rotate(5); } }
         */
    }

    @Override
    public void mouseDblClicked(GraphicObject graphicObject, Event event) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseMoved(GraphicObject graphicObject, Event event) {
        int newX, newY, xMove, yMove;

        if (this.holding == true) {
            newX = event.getClientX();
            newY = event.getClientY();
            xMove = newX - this.x;
            yMove = newY - this.y;

            if (rnaSet == true)
            {
            	rna.translate(xMove, yMove);
            }

            if(rnaSet == false)
            {
            // Window.alert("weee");
            	this.reDrawAllTranslation(xMove, yMove);
            }

            this.x = newX;
            this.y = newY;
        }
    }

    @Override
    public void mousePressed(GraphicObject graphicObject, Event event) {
        this.x = event.getClientX();
        this.y = event.getClientY();

        if (graphicObject != null)
        {
	        for (RNAssDrawable rna : this.dr.pool.getAll()) {
	            if (rna.ContainVirtualGroup(graphicObject.getGroup())) {
	                this.rna = rna;
	                rnaSet = true;
	            }
	        }
        }
        this.holding = true;
    }

    @Override
    public void mouseReleased(GraphicObject graphicObject, Event event) {
        int newX, newY, xMove, yMove;

        this.holding = false;
        
        

        newX = event.getClientX();
        newY = event.getClientY();
        xMove = newX - this.x;
        yMove = newY - this.y;
        
        if (rnaSet == true)
        {
        	rna.translate(xMove, yMove);
        }

        if(rnaSet == false)
        {
        // Window.alert("weee");
        	this.reDrawAllTranslation(xMove, yMove);
        }
        //this.reDrawAllTranslation(xMove, yMove);
        
        this.rna = null;
        this.rnaSet = false;
    }

    private void reDrawAllTranslation(int moveX, int moveY) {

        for (RNAssDrawable po : this.dr.pool.getAll()) {
            po.translate(moveX, moveY);
        }
    }

    public boolean isHolding() {
        return holding;
    }
}
